﻿using Azylee.Core.DataUtils.CollectionUtils;
using Azylee.Core.WindowsUtils.InfoUtils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace JustMetClient.Commons
{
    public static partial class R
    {
        public static class NetInfo
        {
            /// <summary>
            /// 获取本机IP地址
            /// </summary>
            internal static string IP = "";
            internal static string Mac = "";
            internal static string Gateway = "";
            /// <summary>
            /// 获取本机所有网卡信息
            /// </summary>
            internal static List<Tuple<string, string, string, string, string>> AllCards { get; set; }
            /// <summary>
            /// 刷新当前网卡相关信息
            /// </summary>
            /// <param name="ip"></param>
            internal static void Refresh(string ip)
            {
                try
                {
                    AllCards = ComputerInfoTool.NetworkCardInfo();
                    if (!string.IsNullOrWhiteSpace(ip))
                    {
                        IP = ip;
                        if (ListTool.HasElements(AllCards))
                        {
                            var current = AllCards.FirstOrDefault(x => x.Item4 == IP);
                            if (current != null)
                            {
                                IP = current.Item4;
                                Mac = NetCardInfoTool.ShortMac(current.Item3);
                                Gateway = current.Item5;
                            }
                        }
                    }
                }
                catch { }
            }
            /// <summary>
            /// 设置默认网卡IP地址
            /// </summary>
            internal static void Default()
            {
                string ip = "";
                try
                {
                    AllCards = ComputerInfoTool.NetworkCardInfo();
                    if (ListTool.HasElements(AllCards))
                    {
                        foreach (var card in AllCards)
                        {
                            string _desc = card.Item2.ToLower();
                            if (!_desc.Contains("ssl") && !_desc.Contains("vpn") &&
                                !_desc.Contains("svn") && !_desc.Contains("virtual"))
                            {
                                ip = card.Item4;//设置IP

                                if (_desc.Contains("pci"))//设置板载网卡IP
                                {
                                    ip = card.Item4;
                                    break;
                                }
                            }
                        }
                    }
                }
                catch { }
                Refresh(ip);
            }
        }
    }
}
